/**
 * Zhznet.com Inc.
 * Copyright (c) 2004-2011 All Rights Reserved.
 */
package com.liwei.httpclient.demo;

import java.io.File;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.MultipartPostMethod;

/**
 * 用来演示提交文件的例子
 * @author Liwei
 * @version $Id: PostXMLClient.java, v 0.1 2011-1-22 下午09:24:34 Liwei Exp $
 */
public class PostFileClient {

    private static final String targetURL      = "http://222.91.160.251:8080/OCRWeb/SocolarCaptcha";
    private static final String targetFilePath = "D:/liweisvn/liwei-oa-parse/20110121214204453.gif";

    public static void main(String[] args) throws Exception {

        MultipartPostMethod filePost = new MultipartPostMethod(targetURL);

        filePost.addParameter("userfile", new File(targetFilePath));

        filePost.addParameter("senderid", "test");

        HttpClient client = new HttpClient();

        // 由于要上传的文件可能比较大 , 因此在此设置最大的连接超时时间 
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

        int status = client.executeMethod(filePost);

        System.out.println("Response body: ");
        System.out.println(filePost.getResponseBodyAsString());

        filePost.releaseConnection();
    }
}
